import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

//多线程代码案例中的阻塞队列  BlockingQueue 就是标准库提供的阻塞队列
public class Demo1 {
    public static void main(String[] args) {
        BlockingQueue<Integer> queue=new ArrayBlockingQueue<>(10);//设置大小为10
        //这是一个阻塞队列
        //然后实现生产者消费者模型
        //这是生产者
        Thread t1=new Thread(()->{
            int count=1;
            while(true){
                try {
                    queue.put(count);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println("生产者生产了: "+count);
                count++;
            }
        });
        //这是消费者,每隔一秒钟消费一个元素.
        Thread t2=new Thread(()->{
            int value;
            while(true){
                try {
                    value=queue.take();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                /*try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }*/
                System.out.println("消费者消费了: "+value);
            }
        });
        t1.start();
        t2.start();
    }
}
